import Vue from 'vue'
import Vuex from 'vuex'
import { router } from '../../router'
import { getSession, setSession } from '@/utils/session'
import { openApiSysBaseInfoQuery } from '@/api/openApi'
Vue.use(Vuex)

const state = {
	webinfo: JSON.parse(getSession('webinfo')),
	buttons: getSession('buttons') ? getSession('buttons').split(',') : '',
	_event: '',
}

const getters = {
	webName(state) {
		if (!state.webinfo) return
		return state.webinfo.filter((v) => v.code == 'webName')[0].value
	},
	copyright(state) {
		if (!state.webinfo) return
		return state.webinfo.filter((v) => v.code == 'copyright')[0].value
	},
	serveInfo(state) {
		if (!state.webinfo) return
		return state.webinfo.filter((v) => v.code == 'serveInfo')[0].value
	},
	logo(state) {
		if (!state.webinfo) return
		let data = state.webinfo.filter((v) => v.code == 'logo')
		return data.length > 0 && data[0].hasOwnProperty('sysAttachment') ?
			data[0].sysAttachment.httpUrl :
			'--'
	},
	loginBg(state) {
		if (!state.webinfo) return
		let loginInfo = state.webinfo.filter((v) => v.code == 'loginBg')
		if (!loginInfo.length || !loginInfo[0].hasOwnProperty('sysAttachment'))
			return
		return loginInfo[0].sysAttachment.httpUrl
	},
	headPic(state) {
		if (!state.webinfo) return
		let data = state.webinfo.filter((v) => v.code == 'headPic')
		return data.length > 0 && data[0].hasOwnProperty('sysAttachment') ?
			data[0].sysAttachment.httpUrl :
			'--'
	},
	articleImg(state) {
		if (!state.webinfo) return
		let data = state.webinfo.find((v) => v.code == 'articleImg')
		return !!data && data.hasOwnProperty('sysAttachment') ? data.sysAttachment.httpUrl : '--'
	},
	bookImg(state) {
		if (!state.webinfo) return
		let data = state.webinfo.filter((v) => v.code == 'bookImg')
		return data.length > 0 && data[0].hasOwnProperty('sysAttachment') ? data[0].sysAttachment.httpUrl : '--'
	},
	telephone(state) {
		if (!state.webinfo) return
		return state.webinfo.filter((v) => v.code == 'telephone')[0].value
	},
	email(state) {
		if (!state.webinfo) return
		return state.webinfo.filter((v) => v.code == 'email')[0].value
	},
	record(state) {
		if (!state.webinfo) return
		return state.webinfo.filter((v) => v.code == 'record')[0].value
	},
	buttons(state) {
		if (!state.buttons) return
		return state.buttons
	},

	windowWidth() {
		return document.documentElement.clientWidth || document.body.clientWidth
	},
	windowHeight() {
		return document.documentElement.clientHeight || document.body.clientHeight
	},
	defaultImg() {
		return require('@/assets/no_pic.png')
	},
}

const actions = {
	setwebinfo({ commit }) {
		return new Promise((resolve, reject) => {
			let webinfo = JSON.parse(getSession('webinfo'));
			if (webinfo) {
				let info = webinfo.find(item => item.code === 'webName');
				document.title = info.value || '冠兴科技公司';
				resolve(webinfo)
			} else {
				openApiSysBaseInfoQuery({ page: 1, limit: 99 })
					.then((res) => {
						let { data } = res.data;
						console.log(data);
						let info = data.find(item => item.code === 'webName');
						commit('SETWEBINFO', data);
						console.log(info);
						document.title = info.value || '冠兴科技公司';
						resolve(data)
					})
					.catch((error) => {
						router.replace({ name: 'maintained' })
						reject(error)
					})
			}
		})
	},
}

const mutations = {
	SETWEBINFO: (state, d) => {
		if (!d) return
		setSession('webinfo', JSON.stringify(d))
		state.webinfo = d
	},
}

export default {
	state,
	getters,
	actions,
	mutations,
}